package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DialogUtil;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UserPerfectInformationActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.ll_dissmiss)
    LinearLayout llDissmiss;
    @Bind(R.id.et_user_perfect_company)
    EditText etUserPerfectCompany;
    @Bind(R.id.et_user_perfect_company_addr)
    EditText etUserPerfectCompanyAddr;
    @Bind(R.id.tv_user_perfect_choose_industry)
    TextView tvUserPerfectChooseIndustry;
    @Bind(R.id.ll_user_perfect_choose_industry)
    LinearLayout llUserPerfectChooseIndustry;
    @Bind(R.id.et_user_perfect_addr)
    EditText etUserPerfectAddr;
    @Bind(R.id.et_user_perfect_main_product)
    EditText etUserPerfectMainProduct;
    @Bind(R.id.et_user_perfect_person_num)
    EditText etUserPerfectPersonNum;
    @Bind(R.id.et_user_perfect_yield)
    EditText etUserPerfectYield;
    @Bind(R.id.et_user_perfect_contacts)
    EditText etUserPerfectContacts;
    @Bind(R.id.et_user_perfect_phone)
    EditText etUserPerfectPhone;
    @Bind(R.id.et_user_perfect_company_website)
    EditText etUserPerfectCompanyWebsite;
    @Bind(R.id.ll_user_perfect_choose_idcard)
    LinearLayout llUserPerfectChooseIdcard;
    @Bind(R.id.ll_user_perfect_choose_company_pic)
    LinearLayout llUserPerfectChooseCompanyPic;
    @Bind(R.id.ll_user_perfect_choose_certificate)
    LinearLayout llUserPerfectChooseCertificate;
    @Bind(R.id.tv_user_perfect_date)
    TextView tvUserPerfectDate;
    @Bind(R.id.ll_user_perfect_date)
    LinearLayout llUserPerfectDate;
    private StringBuffer stringBuffer = new StringBuffer();

    private ArrayList<String> photoIds=new ArrayList<>();
    private ArrayList<String> companyphotos=new ArrayList<>();
    private ArrayList<String> certifyphotos=new ArrayList<>();
    private ArrayList<String> photoIdFileKeys=new ArrayList<>();
    private ArrayList<String> companyphotoFileKeys=new ArrayList<>();
    private ArrayList<String> certifyphotoFileKeys=new ArrayList<>();
    private ArrayList<File> photoIdFiles=new ArrayList<>();
    private ArrayList<File> companyphotoFiles=new ArrayList<>();
    private ArrayList<File> certifyphotoFiles=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_perfect_information);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_perfect_information, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        initEvent();
    }

    private void initEvent() {
        llDissmiss.setOnClickListener(this);
        llUserPerfectChooseIndustry.setOnClickListener(this);
        llUserPerfectChooseIdcard.setOnClickListener(this);
        llUserPerfectChooseCertificate.setOnClickListener(this);
        llUserPerfectChooseCompanyPic.setOnClickListener(this);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        switch (event.msg){
            case "id":
                photoIdFiles.clear();
                photoIdFileKeys.clear();
                photoIds=event.photos;
                for (int i = 0; i < photoIds.size(); i++) {
                    photoIdFiles.add(new File(BitmapUtils.scal(photoIds.get(i))));
                    photoIdFileKeys.add("Idphoto");
                }
                break;
            case "company":
                companyphotoFiles.clear();
                companyphotoFileKeys.clear();
                companyphotos=event.photos;
                for (int i = 0; i < companyphotos.size(); i++) {
                    companyphotoFiles.add(new File(BitmapUtils.scal(photoIds.get(i))));
                    companyphotoFileKeys.add("Companyphoto"+(i+1));
                }
                break;
            case "certify":
                certifyphotoFiles.clear();
                certifyphotoFileKeys.clear();
                certifyphotos=event.photos;
                for (int i = 0; i < certifyphotos.size(); i++) {
                    certifyphotoFiles.add(new File(BitmapUtils.scal(photoIds.get(i))));
                    certifyphotoFileKeys.add("Certifyphoto"+(i+1));
                }
                break;
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_user_perfect_choose_industry:
                String[] title = {"服装制造", "贸易公司", "面料", "辅料", "货运代理", "设备制造"};
                List<String> titles = new ArrayList<>();
                for (int i = 0; i < title.length; i++) {
                    titles.add(title[i]);
                }
                DialogAdapter adapter = new DialogAdapter(this, titles);
                DialogUtil.showDialog(this, adapter, new OnItemClickListener() {
                            @Override
                            public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                stringBuffer.append(item.toString());
                                tvUserPerfectChooseIndustry.setText(stringBuffer);
                                stringBuffer.append(",");
                                dialog.dismiss();
                            }
                        }
                );
                break;
            case R.id.ll_dissmiss:
                llDissmiss.setVisibility(View.GONE);
                break;
            case R.id.ll_user_perfect_choose_idcard:
                Intent intent=new Intent(this, ChoosePhotosActivity.class);
                intent.putExtra("photos",photoIds);
                intent.putExtra("num",1);
                intent.putExtra("what",1);
                startActivity(intent);
                break;
            case R.id.ll_user_perfect_choose_company_pic:
                Intent intent1=new Intent(this, ChoosePhotosActivity.class);
                intent1.putExtra("photos",companyphotos);
                intent1.putExtra("what",2);
                startActivity(intent1);
                break;
            case R.id.ll_user_perfect_choose_certificate:
                Intent intent2=new Intent(this, ChoosePhotosActivity.class);
                intent2.putExtra("photos",certifyphotos);
                intent2.putExtra("what",3);
                startActivity(intent2);
                break;
        }
    }
}
